package pl.s7n.paint.commands;

import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.awt.image.Raster;

import pl.s7n.paint.DrawableCanvas;
import pl.s7n.paint.utils.FloodFill;

public class FloodFillStrokeCmd extends AbstractStrokeCmd {

  BufferedImage image;
  Raster        oldImage;
  Raster        newImage;
  
  private DrawableCanvas parent;

  public FloodFillStrokeCmd() { };
  
  public void setParent(DrawableCanvas parent) {
    this.parent = parent;
  }
  
  @Override
  public void draw(Graphics2D g) {
    super.draw(g);
    if (image != null) {
      FloodFill floodFill = new FloodFill(oldImage);
      floodFill.fill(start.x, start.y, tool.getColor());
      //newImage = floodFill.getImage().getData();
      //image.setData(newImage);
      
      BufferedImage temp = floodFill.getImage();
      Shape prevClip = g.getClip();
      
      int w = oldImage.getWidth();
      int h = oldImage.getHeight();
      
      Shape newClip = new Rectangle2D.Float(0, 0, w, h);
      g.setClip(newClip);
      
      g.drawRenderedImage(temp, new AffineTransform());
      g.setClip(prevClip);
    }
  }

  @Override
  public String toString() {
    return "fill";
  }

  public void setCurrentImageRef(BufferedImage image) {
    this.image = image;
    this.oldImage = image.getData();
  }

}
